package com.leon.notepad.content;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.TieBean;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.leon.notepad.R;
import com.luck.picture.lib.permissions.RxPermissions;
import com.leon.notepad.content.adapter.MyNotepadRecyclerAdapter;
import com.leon.notepad.content.bean.NotepadBean;
import com.leon.notepad.content.utils.WrapContentLinearLayoutManager;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.tv_bt_1)
    TextView tvBt1;
    @BindView(R.id.tv_bt_2)
    TextView tvBt2;
    @BindView(R.id.tv_bt_0)
    TextView tvBt0;
    @BindView(R.id.ll_bt)
    LinearLayout llBt;

    private MyNotepadRecyclerAdapter adapter;
    private List<NotepadBean> mList = new ArrayList<>();
    private static final int REQUEST_SETTING_MONEY = 1011;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        List<NotepadBean> list = LitePal.order("time desc").find(NotepadBean.class);
        mList.addAll(list);

        //设置布局管理器
        recyclerview.setLayoutManager(new WrapContentLinearLayoutManager(this, 1));
        adapter = new MyNotepadRecyclerAdapter(mList, this);
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new MyNotepadRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(MainActivity.this, DetailsNotepadActivity.class);
                intent.putExtra("item", mList.get(position));
                startActivityForResult(intent, REQUEST_SETTING_MONEY);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                List<TieBean> datass = new ArrayList<>();
                datass.add(new TieBean("修改"));
                datass.add(new TieBean("删除"));
                DialogUIUtils.showSheet(MainActivity.this, datass,
                        "取消", Gravity.BOTTOM, true, true, new DialogUIItemListener() {
                            @Override
                            public void onItemClick(CharSequence text, int i) {
                                Log.e("1111", "!!!!!!!!" + position);
                                if (i == 1) {
                                    LitePal.delete(NotepadBean.class, mList.get(position).getId());
                                    mList.remove(position);
                                    adapter.notifyDataSetChanged();
                                } else if (i == 0) {
                                    Intent intent = new Intent(MainActivity.this, AddNotepadActivity.class);
                                    intent.putExtra("item", mList.get(position));
                                    intent.putExtra("type", "modify");
                                    startActivityForResult(intent, REQUEST_SETTING_MONEY);
                                }
                            }
                        }).show();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.item_remind:
                RxPermissions rxPermission = new RxPermissions(MainActivity.this);
                rxPermission.request(Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR)
                        .subscribe(granted -> {
                            Log.i("cxw", "申请结果:" + granted);
                            if (granted) {// 用户已经同意该权限
                                Log.e("!!!", "2131");
                                Intent intent = new Intent(MainActivity.this, RemindActivity.class);
                                startActivity(intent);
                            } else {
                                Toast.makeText(MainActivity.this, "请授权，否则无法使用", Toast.LENGTH_SHORT).show();
                            }
                        });

                break;
            case R.id.item_statistical:
                startActivity(new Intent(MainActivity.this, StatisticalActivity.class));
                break;
            case R.id.item_month:
                startActivity(new Intent(MainActivity.this, MonthActivity.class));
                break;
            default:
                break;
        }
        return true;
    }

    @OnClick(R.id.iv_add)
    public void onClick() {
        RxPermissions rxPermission = new RxPermissions(MainActivity.this);
        rxPermission.request(Manifest.permission.RECORD_AUDIO, Manifest.permission.WAKE_LOCK)
                .subscribe(granted -> {
                    Log.i("cxw", "申请结果:" + granted);
                    if (granted) {// 用户已经同意该权限
                        startActivityForResult(new Intent(MainActivity.this, AddNotepadActivity.class), REQUEST_SETTING_MONEY);
                    } else {
                        Toast.makeText(MainActivity.this, "请授权，否则无法使用", Toast.LENGTH_SHORT).show();
                    }
                });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SETTING_MONEY && resultCode == RESULT_OK) {
            mList.clear();
            List<NotepadBean> list = LitePal.order("time desc").find(NotepadBean.class);
            tvBt1.setTextColor(getResources().getColor(R.color.white));
            tvBt1.setBackgroundColor(getResources().getColor(R.color.purple_500));
            tvBt2.setTextColor(getResources().getColor(R.color.black));
            tvBt2.setBackgroundColor(getResources().getColor(R.color.white));
            tvBt0.setTextColor(getResources().getColor(R.color.black));
            tvBt0.setBackgroundColor(getResources().getColor(R.color.white));
            mList.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

    @OnClick(R.id.tv_search)
    public void onClickSearch() {
        String input = etInput.getText().toString();
        List<NotepadBean> list;
        if (input.equals("")) {
            list = LitePal.order("time desc").find(NotepadBean.class);
        } else {
            list = LitePal.where("title like ? ", "%" + input + "%").order("time desc").find(NotepadBean.class);
        }
        tvBt1.setTextColor(getResources().getColor(R.color.white));
        tvBt1.setBackgroundColor(getResources().getColor(R.color.purple_500));
        tvBt2.setTextColor(getResources().getColor(R.color.black));
        tvBt2.setBackgroundColor(getResources().getColor(R.color.white));
        tvBt0.setTextColor(getResources().getColor(R.color.black));
        tvBt0.setBackgroundColor(getResources().getColor(R.color.white));
        mList.clear();
        mList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @OnClick({R.id.tv_bt_1, R.id.tv_bt_2, R.id.tv_bt_0})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_bt_1:
                tvBt1.setTextColor(getResources().getColor(R.color.white));
                tvBt1.setBackgroundColor(getResources().getColor(R.color.purple_500));
                tvBt2.setTextColor(getResources().getColor(R.color.black));
                tvBt2.setBackgroundColor(getResources().getColor(R.color.white));
                tvBt0.setTextColor(getResources().getColor(R.color.black));
                tvBt0.setBackgroundColor(getResources().getColor(R.color.white));
                mList.clear();
                List<NotepadBean> list = LitePal.where("type = ?", "0").order("time desc").find(NotepadBean.class);
                mList.addAll(list);
                adapter.notifyDataSetChanged();
                break;
            case R.id.tv_bt_2:
                tvBt2.setTextColor(getResources().getColor(R.color.white));
                tvBt2.setBackgroundColor(getResources().getColor(R.color.purple_500));
                tvBt1.setTextColor(getResources().getColor(R.color.black));
                tvBt1.setBackgroundColor(getResources().getColor(R.color.white));
                tvBt0.setTextColor(getResources().getColor(R.color.black));
                tvBt0.setBackgroundColor(getResources().getColor(R.color.white));
                mList.clear();
                List<NotepadBean> list1 = LitePal.where("type = ?", "1").order("time desc").find(NotepadBean.class);
                mList.addAll(list1);
                adapter.notifyDataSetChanged();
                break;
            case R.id.tv_bt_0:
                tvBt0.setTextColor(getResources().getColor(R.color.white));
                tvBt0.setBackgroundColor(getResources().getColor(R.color.purple_500));
                tvBt1.setTextColor(getResources().getColor(R.color.black));
                tvBt1.setBackgroundColor(getResources().getColor(R.color.white));
                tvBt2.setTextColor(getResources().getColor(R.color.black));
                tvBt2.setBackgroundColor(getResources().getColor(R.color.white));
                mList.clear();
                List<NotepadBean> list2 = LitePal.order("time desc").find(NotepadBean.class);
                mList.addAll(list2);
                adapter.notifyDataSetChanged();
                break;
        }
    }
}